package com.frxuexi.park2301

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.park2301.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    var timer:Timer?=null
    var num=5
    override fun initData() {

    }

    override fun initView() {
        if(SPUtils.getInstance().getBoolean("isIn",false)){
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            finish()
        }
        timer= Timer()
        lifecycleScope.launch(Dispatchers.IO) {
            timer!!.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.splashTime.setText("${num}s")
                    if(num==0){
                        start()
                    }
                    num--
                }
            },0,1000)
        }

        binding.splashSkip.setOnClickListener {
            start()
        }

    }

    fun start(){
        timer!!.cancel()
        SPUtils.getInstance().put("isIn",true)
        ARouter.getInstance().build("/app/LoginActivity").navigation()
        finish()
    }


    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer?.cancel()
            timer=null
        }
    }

}